
import request from "@/utils/request";

export default {
	total: {
		spower: totalPower,
		sminer: totalSminer,
		file: totalFile,
		extrinsics: totalExtrinsics,
		events: totalEvents,
		transfer: totalTransfer,
		moneyedAccount: totalHasMoneyAccount,
		stakingSminer: totalStakingSminer,
		nominator: totalNominator,
		validator: totalValidator,
		issuance: totalIssuance,
		staking: totalStaking,
		accountShange: totalAccountShange,
		consensus: totalConsensus,
		storage: totalSstorage
	},
	trend: {
		transfer: {
			byday: transferByday,
			byweek: transferByweek,
			bymonth: transferBymonth,
		},
		spower: {
			byday: spowerByday
		}
	},
	block: {
		detail: blockDetail,
		list: blockList
	},
	extrinsics: {
		list: extrinsicsList,
		detail: extrinsicsDetail
	},
	transfer: {
		list: transferList
	},
	event: {
		list: eventList,
		listByExtrinsicHash: eventListByExtrinsicHash
	},
	account: {
		list: accountList,
		info: accountInfo,
		territorys: accountTerritoryList,
		files: accountFileList,
		types: accountTypes,
		nominators: accountNominatorList
	},
	sminer: {//存储矿工
		list: sminerList,
		listByAccount: sminerListByAccount,
		revenues: sminerRevenueList,
		top100Revenues: top100Revenues,
	},
	eminer: {//收益账户
		list: eminerList
	},
	file: {
		// list: fileList,
		detail: fileDetail,
		holders: fileHolders
	},
	rpc: {
		list: rpcList
	},
	storage: {
		gateway: gatewayList,
		price: getPrice
	},
	challenge: {
		list: challengeList
	},
	consensu: {
		list: consensuList
	}
};

//for total
function totalPower() {
	return request.get("/spower/query/total/capacity"); //spower
}
function totalSminer() {
	return request.get("/sminer/query/count"); //sminer totle count
}
function totalFile() {
	return request.get("/file/query/count"); //文件总数
}
function totalExtrinsics() {
	return request.get("/block/query/extrinsics/count");
}
function totalEvents() {
	return request.get("/block/query/events/count");
}
function totalTransfer() {
	return request.get("/transfer/query/count");
}
//查询有余额的账户数量
function totalHasMoneyAccount() {
	return request.get("/account/query/moneyed/count");
}
//查询所有存储矿工的质押总数
function totalStakingSminer() {
	return request.get("/sminer/query/staking/all");
}
//查询提名人数量
function totalNominator() {
	return request.get("/chain/query/nominator/count");
}
//查询验证者数量
function totalValidator() {
	return request.get("/chain/query/validator/count");
}
/**
* @function 查询issuance数量
*/
async function totalIssuance() {
	return request.get(`/chain/query/issuance`);
}
/**
* @function 查询共识节点和存储节点的总质押
*/
async function totalStaking() {
	return request.get(`/chain/query/staking/total`);
}
/**
* @function 查询有钱账户和有交易的账户数量以及他们与昨天的变化
*/
async function totalAccountShange() {
	return request.get(`/account/query/change`);
}
/**
* @function 查询共识节点数量
*/
async function totalConsensus() {
	return request.get(`/chain/query/consensus/count`);
}
/**
* @function 存储空间
*/
async function totalSstorage() {
	return request.get(`/spower/query/storage`);
}



//trend
/**
* @function 按天查询转账数量
*/
async function transferByday({ pageindex, pagesize }) {
	let res = await request.get(`/transfer/query/byday?pageindex=${pageindex}&pagesize=${pagesize}`);
	return formatList(res);
}
/**
* @function 按周查询转账数量
*/
async function transferByweek({ pageindex, pagesize }) {
	let res = await request.get(`/transfer/query/byweek?pageindex=${pageindex}&pagesize=${pagesize}`);
	return formatList(res);
}
/**
* @function 按月查询转账数量
*/
async function transferBymonth({ pageindex, pagesize }) {
	let res = await request.get(`/transfer/query/bymonth?pageindex=${pageindex}&pagesize=${pagesize}`);
	return formatList(res);
}
/**
* @function 查询每天的总空间数据
*/
async function spowerByday({ pageindex, pagesize }) {
	let res = await request.get(`/spower/query/byday?pageindex=${pageindex}&pagesize=${pagesize}`);
	return formatList(res);
}


//for block
function blockDetail(blockId, hash) {
	if (hash) {
		return request.get("/block/query?hash=" + hash);
	}
	return request.get("/block/query?id=" + blockId);
}
async function blockList({ pageindex, pagesize }) {
	let res = await request.get(`/block/query/list?pageindex=${pageindex}&pagesize=${pagesize}`);
	return formatList(res);
}

//for extrinsics
async function extrinsicsList({ blockId, accountId, pageindex, pagesize }) {
	let url = '/block/query/extrinsics?';
	let params = [];
	if (blockId) {
		params.push('id=' + blockId);
	}
	if (accountId) {
		params.push('account=' + accountId);
	}
	if (pageindex) {
		params.push('pageindex=' + pageindex);
	}
	if (pagesize) {
		params.push('pagesize=' + pagesize);
	}
	url += params.join('&');
	let res = await request.get(url);
	return formatList(res);
}
function extrinsicsDetail(hash) {
	return request.get("/block/query/extrinsics/details?hash=" + hash);
}


//for transfer
async function transferList({ accountId, pageindex, pagesize }) {
	let url = '/transfer/query?';
	let params = [];
	if (accountId) {
		params.push('Acc=' + accountId);
	}
	if (pageindex) {
		params.push('pageindex=' + pageindex);
	}
	if (pagesize) {
		params.push('pagesize=' + pagesize);
	}
	url += params.join('&');
	let res = await request.get(url);
	return formatList(res);
}

//for events
async function eventList({ blockId, pageindex, pagesize }) {
	let url = '/block/query/events?';
	let params = [];
	if (blockId) {
		params.push('id=' + blockId);
	}
	if (pageindex) {
		params.push('pageindex=' + pageindex);
	}
	if (pagesize) {
		params.push('pagesize=' + pagesize);
	}
	url += params.join('&');
	let res = await request.get(url);
	return formatList(res);
}
async function eventListByExtrinsicHash({ extrinsicHash }) {
	let res = await request.get(`/block/query/extrinsics/events?extrinsic_hash=${extrinsicHash}`);
	return formatList(res);
}

//account
async function accountList({ pageindex, pagesize }) {
	let res = await request.get(`/account/query/list?pageindex=${pageindex}&pagesize=${pagesize}`);
	let ret = formatList(res);
	if (ret.msg == 'ok' && ret.data && ret.data.length > 0) {
		ret.data.forEach((t, i) => t.rank = i + 1 + pagesize * (pageindex - 1));
	}
	return ret;
}
async function accountInfo(account) {
	let res = await request.get(`/chain/query/account?Acc=${account}`);
	return res;
}
//查询账户拥有的文件列表
async function accountFileList({ accountId, pageindex, pagesize }) {
	let url = '/file/user/list?';
	let params = [];
	if (accountId) {
		params.push('Acc=' + accountId);
	}
	if (pageindex) {
		params.push('pageindex=' + pageindex);
	}
	if (pagesize) {
		params.push('pagesize=' + pagesize);
	}
	url += params.join('&');
	let res = await request.get(url);
	return formatList(res);
}
async function accountTerritoryList({ accountId, pageindex, pagesize }) {
	let url = '/territory/user/list?';
	let params = [];
	if (accountId) {
		params.push('Acc=' + accountId);
	}
	if (pageindex) {
		params.push('pageindex=' + pageindex);
	}
	if (pagesize) {
		params.push('pagesize=' + pagesize);
	}
	url += params.join('&');
	let res = await request.get(url);
	return formatList(res);
}
// //space
// async function accountSpaceInfo({ accountId, pageindex, pagesize }) {
// 	let url = '/user/query?';
// 	let params = [];
// 	if (accountId) {
// 		params.push('Acc=' + accountId);
// 	}
// 	if (pageindex) {
// 		params.push('pageindex=' + pageindex);
// 	}
// 	if (pagesize) {
// 		params.push('pagesize=' + pagesize);
// 	}
// 	url += params.join('&');
// 	let res = await request.get(url);
// 	return formatList(res);
// }
//#查询account type
async function accountTypes() {
	let res = await request.get("/account/query/type");
	return res;
}
//查询共识节点的提名人列表
async function accountNominatorList({ accountId, pageindex, pagesize }) {
	let url = '/chain/query/validator/nominators?';
	let params = [];
	if (accountId) {
		params.push('Acc=' + accountId);
	}
	if (pageindex) {
		params.push('pageindex=' + pageindex);
	}
	if (pagesize) {
		params.push('pagesize=' + pagesize);
	}
	url += params.join('&');
	let res = await request.get(url);
	return formatList(res);
}



//sminer
async function sminerList({ accountId, pageindex, pagesize }) {
	let url = '/sminer/query/list?';
	let params = [];
	if (accountId) {
		params.push('Acc=' + accountId);
	}
	if (pageindex) {
		params.push('pageindex=' + pageindex);
	}
	if (pagesize) {
		params.push('pagesize=' + pagesize);
	}
	url += params.join('&');
	let res = await request.get(url);
	return formatList(res);
}
//查询收益账户对应的全部矿
async function sminerRevenueList({ accountId, pageindex, pagesize }) {
	let url = '/sminer/query/revenue/list?';
	let params = [];
	if (accountId) {
		url = '/sminer/query/revenue?';
		params.push('Acc=' + accountId);
	}
	if (pageindex) {
		params.push('pageindex=' + pageindex);
	}
	if (pagesize) {
		params.push('pagesize=' + pagesize);
	}
	url += params.join('&');
	let res = await request.get(url);
	return formatList(res, pageindex);
}
async function top100Revenues({ accountId, pageindex, pagesize }) {
	let ret = await sminerRevenueList({ accountId, pageindex, pagesize });
	if (ret.total && ret.total > 100) {
		ret.total = 100;
	}
	return ret;
}

async function sminerListByAccount(accountId) {
	let url = '/sminer/query?Acc=' + accountId;
	let res = await request.get(url);
	return formatList(res);
}


async function eminerList({ accountId, pageindex, pagesize }) {
	let url = '/eminer/query?';
	let params = [];
	if (accountId) {
		params.push('Acc=' + accountId);
	}
	if (pageindex) {
		params.push('pageindex=' + pageindex);
	}
	if (pagesize) {
		params.push('pagesize=' + pagesize);
	}
	url += params.join('&');
	let res = await request.get(url);
	return formatList(res);
}

//file
// async function fileList({ accountId, pageindex, pagesize }) {
// 	let url = '/file/query?';
// 	let params = [];
// 	if (accountId) {
// 		params.push('Acc=' + accountId);
// 	}
// 	if (pageindex) {
// 		params.push('pageindex=' + pageindex);
// 	}
// 	if (pagesize) {
// 		params.push('pagesize=' + pagesize);
// 	}
// 	url += params.join('&');
// 	let res = await request.get(url);
// 	return formatList(res);
// }
async function fileDetail(fid) {
	let url = '/file/query/details?Fid=' + fid.replace('0x', '');
	let res = await request.get(url);
	return formatList(res);
}
async function fileHolders({ fid, pageindex, pagesize }) {
	let url = '/file/query/holders?';
	let params = [];
	if (fid) {
		params.push('Fid=' + fid);
	}
	if (pageindex) {
		params.push('pageindex=' + pageindex);
	}
	if (pagesize) {
		params.push('pagesize=' + pagesize);
	}
	url += params.join('&');
	let res = await request.get(url);
	return formatList(res);
}

//查询rpc地址列表
async function rpcList({ pageindex, pagesize }) {
	let res = await request.get(`/chain/query/rpc/list?pageindex=${pageindex}&pagesize=${pagesize}`);
	let ret = formatList(res);
	return ret;
}
async function gatewayList({ pageindex, pagesize }) {
	let res = await request.get(`/gateway/query/list?pageindex=${pageindex}&pagesize=${pagesize}`);
	let ret = formatList(res);
	return ret;
}
async function getPrice() {
	return 30;
}

/**
* @function 查询存储矿工的挑战列表
*/
async function challengeList({ accountId, pageindex, pagesize }) {
	let url = '/challenge/query?';
	let params = [];
	if (accountId) {
		params.push('Acc=' + accountId);
	}
	if (pageindex) {
		params.push('pageindex=' + pageindex);
	}
	if (pagesize) {
		params.push('pagesize=' + pagesize);
	}
	url += params.join('&');
	let res = await request.get(url);
	return formatList(res);
}

/**
* @function 查询account的共识列表
*/
async function consensuList({ accountId, pageindex, pagesize }) {
	let url = '/chain/query/consensus/list?';
	let params = [];
	if (accountId) {
		params.push('Acc=' + accountId);
	}
	if (pageindex) {
		params.push('pageindex=' + pageindex);
	}
	if (pagesize) {
		params.push('pagesize=' + pagesize);
	}
	url += params.join('&');
	let res = await request.get(url);
	return formatList(res);
}



function formatList(res, pageindex) {
	if (res.msg != 'ok' || !Array.isArray(res.data.content)) {
		return res;
	}
	let ret = { ...res.data };
	ret.msg = res.msg;
	ret.total = res.data.count;
	let data = res.data.content;
	if (pageindex) {
		let n = (pageindex - 1) * 10;
		for (let i = 0; i < data.length; i++) {
			data[i].rank_number = n + i + 1;
		}
	}
	ret.data = data;
	delete ret.count;
	delete ret.content;
	return ret;
}
///sminer/query/count
///block/query/latest
// /transfer/query?Acc=cX...&pageindex=1&pagesize=10
